package com.bub.pay.api.controller;

import com.bub.pay.api.entity.vo.QuerySysPermVo;
import com.bub.pay.core.entity.domain.SysPerm;
import com.bub.pay.core.entity.dto.system.QuerySysPermDTO;
import com.bub.pay.core.entity.vo.user.AddSysPermVo;
import com.bub.pay.core.manager.menu.PermManager;
import com.bub.pay.framework.common.base.Cause;
import com.bub.pay.framework.common.base.Module;
import com.bub.pay.framework.common.exception.GlobalException;
import com.bub.pay.framework.common.util.CollectionUtil;
import com.bub.pay.framework.common.util.StrUtil;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 权限管理
 */
@RestController
@RequestMapping("admin/perm/")
public class PermController extends BaseController {

    @Resource
    private PermManager permManager;

    @RequestMapping("queryPermList")
    @RequiresPermissions("employeeManage:authority:view")
    public Object queryPermList(@RequestBody QuerySysPermVo permVo) {
        Example example = new Example(SysPerm.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("platformType", permVo.getPlatformType()).andEqualTo("delFlag", 0);
        Long permId = permVo.getId();
        if (!ObjectUtils.isEmpty(permId)) {
            criteria.andEqualTo("id", permId);
        }
        example.orderBy("sortNum");
        List<SysPerm> listPerm = permManager.getByExample(example);
        List<QuerySysPermDTO> listDto = new ArrayList<>();
        if (!CollectionUtil.isNullOrEmpty(listPerm)) {
            listDto = listPerm.stream().map(e -> {
                QuerySysPermDTO dto = new QuerySysPermDTO();
                BeanUtils.copyProperties(e, dto);
                return dto;
            }).collect(Collectors.toList());
        }
        return listDto;
    }

    @RequestMapping("addPerm")
    @RequiresPermissions(value = {"employeeManage:authority:addParent", "employeeManage:authority:addChild"}, logical = Logical.OR)
    public Object addMenu(@RequestBody AddSysPermVo permVo) {
        if (!Arrays.asList(new Integer[]{1, 2, 3,4}).contains(permVo.getPlatformType())) {
            throw new GlobalException(Module.admin, Cause.admin_add_menu_platformtype_param_error);
        }
        if (!StrUtil.checkParamByPattern("[\u4e00-\u9fa5a-zA-z0-9]{2,100}", permVo.getName())) {
            throw new GlobalException(Module.admin, Cause.admin_add_menu_name_param_error);
        }
        SysPerm perm = new SysPerm();
        perm.setCreateDate(new Date());
        BeanUtils.copyProperties(permVo, perm);
        int i = permManager.insertSelective(perm);
        if (i == 0) {
            throw new GlobalException(Module.admin, Cause.admin_add_menu_fail);
        }
        return i;
    }

    @RequestMapping("updatePerm")
    @RequiresPermissions("employeeManage:authority:update")
    public Object updateMenu(@RequestBody AddSysPermVo upVo) {
        if (Objects.isNull(upVo.getId())) {
            throw new GlobalException(Module.admin, Cause.admin_update_menu_id_not_platformtype_error);
        }
        SysPerm sysPerm = permManager.getById(upVo.getId());
        if (Objects.isNull(sysPerm)) {
            throw new GlobalException(Module.admin, Cause.admin_update_menu_not_perm_error);
        }
        if (Objects.isNull(upVo.getPlatformType())) {
            if (!Arrays.asList(new Integer[]{1, 2, 3,4}).contains(upVo.getPlatformType())) {
                throw new GlobalException(Module.admin, Cause.admin_add_menu_platformtype_param_error);
            }
        }
        if (!Objects.isNull(upVo.getStatus())) {
            SysPerm perm = new SysPerm();
            perm.setStatus(upVo.getStatus());
            Example perExa = new Example(SysPerm.class);
            perExa.createCriteria().andEqualTo("parentId", upVo.getId()).andEqualTo("delFlag", false);
            permManager.updateByExampleSelective(perm, perExa);
        }
        SysPerm perm = new SysPerm();
        BeanUtils.copyProperties(upVo, perm);
        return permManager.updateByPrimaryKeySelective(perm);
    }

    @RequestMapping("deletePerm")
    @RequiresPermissions("employeeManage:authority:delete")
    public Object deleteMenu(@RequestBody AddSysPermVo upVo) {
        if (Objects.isNull(upVo.getId())) {
            throw new GlobalException(Module.admin, Cause.admin_update_menu_id_not_platformtype_error);
        }
        return permManager.deletePerm(upVo);
    }
}
